package cn.com.ejiakanghu.web.alimini;/**
 * Created by Jhonbo on 2018/5/17.
 */

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.ZhimaMerchantOrderRentCompleteRequest;
import com.alipay.api.request.ZhimaMerchantOrderRentQueryRequest;
import com.alipay.api.response.ZhimaMerchantOrderRentCompleteResponse;
import com.alipay.api.response.ZhimaMerchantOrderRentQueryResponse;
import org.linlinjava.litemall.core.util.DateTimeUtil;

/**
 * 阿里免押金工具类
 *
 * @author houzhibin
 * @create 2018-05-17 22:06
 */
public class AliNoDepositUtil {

    /**
     * 阿里免押金租赁归还设备
     * @return
     * @throws AlipayApiException
     */
    public static ZhimaMerchantOrderRentCompleteResponse returnEquipment(String orderCode,String borrowTime,String returnTime,String payType,double leasePrice,String shopName) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",AliMiniConfig.ALI_MINI_APP_ID,AliMiniConfig.ALI_PRIVATE_KEY,"json","utf-8",AliMiniConfig.ALI_PUBLIC_KEY,"RSA2");  // your_private_key:商户私钥；alipay_public_key:蚂蚁公钥
        ZhimaMerchantOrderRentCompleteRequest request = new ZhimaMerchantOrderRentCompleteRequest();
        if(returnTime.endsWith(".0")) {
            returnTime = returnTime.substring(0,returnTime.length() - 2);
        }
        long diff = DateTimeUtil.compareTime(returnTime,borrowTime);
        if(diff <= 0) {
          returnTime = DateTimeUtil.getFautureTimeByTime(borrowTime,60*1000);
        }
        request.setBizContent("{" +
                " \"order_no\":\""+orderCode+"\"," + // 信用借还订单号
                " \"product_code\":\"w1010100000000002858\"," + // 信用借还的产品码，固定不变
                " \"restore_time\":\""+returnTime+"\"," +          // 物品归还时间
                " \"pay_amount_type\":\""+payType+"\"," +  // 金额类型：RENT(租金)，DAMAGE(赔偿金)
                " \"pay_amount\":\""+leasePrice+"\"," +         // 支付金额
                " \"restore_shop_name\":\""+shopName+"\"" +    // 物品归还门店名称
                " }");
        ZhimaMerchantOrderRentCompleteResponse response = alipayClient.execute(request);
        return response;
    }

    /**
     * 查询阿里的订单信息
     *
     * @return
     * @throws AlipayApiException
     */
    public static String getAliOrderNo(String outOrderNo) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AliMiniConfig.ALI_MINI_APP_ID, AliMiniConfig.ALI_PRIVATE_KEY, "json", "utf-8", AliMiniConfig.ALI_PUBLIC_KEY, "RSA2");
        ZhimaMerchantOrderRentQueryRequest request = new ZhimaMerchantOrderRentQueryRequest();
        request.setBizContent("{" +
                "\"out_order_no\":\"" + outOrderNo + "\"," +
                "\"product_code\":\"w1010100000000002858\"" +
                "  }");
        ZhimaMerchantOrderRentQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            return response.getOrderNo();
        } else {
            return "";
        }
    }

}
